﻿using System;
using System.Collections;
using System.Linq;
using DevExpress.XtraNavBar;

namespace WinForm.Common
{
    /// <summary>
    /// FunctionModule集合类，FunctionModule都存在CollectionBase类的List中
    /// </summary>
    public class FunctionModules:CollectionBase{
        #region Field

        public event EventHandler CurrentModuleChanged;

        #endregion Field

        #region 属性

        public static readonly FunctionModules Instance = new FunctionModules();

        /// <summary>
        /// 当前正在显示的模块
        /// </summary>
        public FunctionModule CurrentModule { get; set; }
        //todo:CurrentModule 属性不能有set功能，可以修改为set自动调用showModule方法
        #endregion 属性

        #region Collection 的基本操作重载
        public FunctionModule this[int index]
        {
            get
            {
                if (List.Count > index)
                    return List[index] as FunctionModule;
                return null;
            }
        }
        public FunctionModule this[string name]
        {
            //get
            //{
            //    foreach (FunctionModule module in this)
            //        if (module.Name.Equals(name))
            //            return module;
            //    return null;
            //}
            get
            { return this.Cast<FunctionModule>().FirstOrDefault(module => module.Name.Equals(name)); }
        }
        public void Add(FunctionModule value)
        {
            if (!List.Contains(value))
                List.Add(value);
        }
       
        /// <summary>
        /// 添加功能模块
        /// </summary>
        /// <param name="group">模块所属的分组名称</param>
        /// <param name="name">模块名称</param>
        /// <param name="description">对模块的描述</param>
        /// <param name="imageName">使用的图像,保存在Image文件下，然后嵌入</param>
        /// <param name="type">该模块对应的用户控件的类型</param>
        public void Add(string group, string name, string description, string imageName,Type type )
        {
            FunctionModule item = new FunctionModule(name, description, group, imageName, type);
            Instance.Add(item);
        }
        public void Remove(FunctionModule value)
        {
            if (List.Contains(value))
                List.Remove(value);
        }
        public void Remove(string name)
        {
            FunctionModule module = Instance[name];
            if (module != null)
                Instance.Remove(module);
        }
        #endregion

        #region FillToNavBar
        /// <summary>
        /// 把模块信息填充到NavBarControl控件中显示
        /// </summary>
        /// <param name="navBar">目标NavBarControl控件</param>
        public void FillToNavBar(NavBarControl navBar)
        {
            FillToNavBar(navBar, NavBarGroupStyle.LargeIconsText, NavBarImage.Small);
        }

        /// <summary>
        /// 把模块信息填充到NavBarControl控件中显示
        /// </summary>
        /// <param name="navBar">目标NavBarControl控件</param>
        /// <param name="groupStyle">分组显示的样式</param>
        /// <param name="groupCaptionImage">图片</param>
        public void FillToNavBar(NavBarControl navBar, NavBarGroupStyle groupStyle, NavBarImage groupCaptionImage)
        {
            if (navBar == null) return;
            navBar.BeginUpdate();
            for (int i = 0; i < Instance.Count; i++)
            {
                FunctionModule module = Instance[i];
                if (module.Group == ResourcesHelper.GetString("GroupAbout")) continue;

                var nbGroup = GetNavBarGroup(navBar, module.Group, groupStyle, groupCaptionImage);
                var item = new NavBarItem
                {
                    Caption = module.Name, 
                    SmallImage = module.SmallImage, 
                    LargeImage = module.LargeImage, 
                    Tag = module
                };
                nbGroup.ItemLinks.Add(new NavBarItemLink(item));
            }
            navBar.EndUpdate();
        }

        /// <summary>
        /// 取得NavBar的分组，如果分组不存在，则创建后返回
        /// </summary>
        /// <param name="navBar"></param>
        /// <param name="groupName"></param>
        /// <param name="groupStyle">创建的分组风格</param>
        /// <param name="groupCaptionImage">穿件的分组图片</param>
        /// <returns></returns>
        private static NavBarGroup GetNavBarGroup(NavBarControl navBar, string groupName, NavBarGroupStyle groupStyle, NavBarImage groupCaptionImage)
        {
            foreach (NavBarGroup group in navBar.Groups)
                if (group.Caption == groupName) return group; NavBarGroup newGroup = navBar.Groups.Add();
            newGroup.Caption = groupName;
            newGroup.Name = groupName;
            newGroup.GroupStyle = groupStyle;
            newGroup.GroupCaptionUseImage = groupCaptionImage;
            return newGroup;
        }
        #endregion

        #region ShowModule

        /// <summary>
        /// 设置名字为moduleName的模块为要显示的（当前）模块
        /// </summary>
        /// <param name="moduleName">要显示的Module名字</param>
        public void SetCurrentModule(string moduleName)
        {
            if (CurrentModule!=null && CurrentModule.Name == moduleName)
                return;
            
            FunctionModule functionModule = Instance[moduleName];
            if (functionModule == null) return;

            //TODO：考虑当前模块数据没有保存的情况，若没有保存首先弹出对话框，请用户确认是否保存，然后才。。。

            //释放正在显示的模块
            if (CurrentModule != null)
            {
                CurrentModule.UserControl.Dispose();
                CurrentModule.UserControl = null;
            }

            //修改当前模块为指定模块
            CurrentModule = functionModule;
            RaiseModuleChanged();
        }

        /// <summary>
        /// 触发模块改变事件
        /// </summary>
        protected static void RaiseModuleChanged()
        {
            if (Instance.CurrentModuleChanged != null)
                Instance.CurrentModuleChanged(Instance, EventArgs.Empty);
        }

        #endregion
    }
}